<template>
  <div class="search-suggestion">
    <van-cell v-for="(item,index) in list" :key="index" icon="search" @click="$emit('search',item)">
      <span slot="title" v-html="textActive(item,searchValue)"></span>
    </van-cell>
  </div>
</template>

<script>
import { getSearchSuggestion } from '@/api/search'
import { textActive } from '@/filter'
import { debounce } from 'lodash'
// debounce(函数，时间)
export default {
  name: 'SearchSuggestion',
  data () {
    return {
      list: []
    }
  },
  props: {
    // 搜索关键字
    searchValue: {
      type: String,
      required: true
    }
  },
  watch: {
    searchValue: {
      immediate: true, // 一开始就触发一次
      handler: debounce(function () {
        this.loadSearchSuggestion()
      }, 200)
    }
  },
  methods: {
    // 获取搜索建议
    async loadSearchSuggestion () {
      const { data: res } = await getSearchSuggestion(this.searchValue)
      this.list = res.data.options
    },
    textActive
  }
}
</script>

<style lang="less" scoped>

</style>
